use ndarray::prelude::*;
use ndarray_rand::{
    RandomExt, SamplingStrategy,
    rand_distr::{StandardNormal, Uniform},
};
use ndarray_stats::{
    HistogramExt,
    histogram::{GridBuilder, strategies::Sqrt},
};
use noisy_float::types::N64;
use rand::seq::IteratorRandom;
use show_image::Color;

fn main() -> anyhow::Result<()> {
    let arr = array![1., 2., 3., 4., 5., 6.];
    println!("1D array: {}", arr);

    let arr2 = array![1., 2.2, 3.3, 4.4, 5., 6.];
    let arr3 = arr + arr2;
    println!("1D array: {}", arr3);

    let arr4 = array![[1., 2., 3.], [4., 5., 6.]];
    println!("arr4: {}", arr4);

    let arr5 = Array::from_elem([2, 1], 1.);
    println!("1D array: {}", arr5);
    println!("{}", arr4 + arr5);

    let arr6: Array2<f64> = Array::eye(3);
    let arr7 = array![[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]];
    println!("1D array: {}", arr7);
    println!("{}", &arr7 * &arr6);

    let arr12 = Array::<f64, _>::ones([2, 3, 2, 2]);
    println!("1D array: {}", arr12);

    let arr13 = Array::random([2, 5], Uniform::new(0., 10.));
    println!("1D array: {}", arr13);

    let arr14 = array![1., 2., 3., 4., 5., 6.];
    let arr15 = arr14.sample_axis(Axis(0), 2, SamplingStrategy::WithoutReplacement);
    println!("1D array: {}", arr15);

    let mut rng = rand::thread_rng();
    let faces = "👩‍🦰👯‍♂️👠😍😍🫦";
    let arr16 = Array::from_shape_vec([2, 2], faces.chars().choose_multiple(&mut rng, 4)).unwrap();
    println!("1D array: {}", arr16);

    let arr17 = Array::<f64, _>::random_using([10000, 2], StandardNormal, &mut rng);
    println!("1D array: {}", arr17);
    let data = arr17.mapv(noisy_float::types::n64);

    let grid = GridBuilder::<Sqrt<N64>>::from_array(&data).unwrap().build();
    let histogram = data.histogram(grid);
    let histogram_matrix = histogram.counts();
    println!("{}", histogram_matrix);
    let data = histogram_matrix.sum_axis(Axis(0));
    println!("{}", data);
    let result = show_image::DrawArea::new_draw_area("./histogram.png", (600, 400))?;
    result
        .generate_chart_context2d("histogram", 0. ..data.len() as f64, 0. ..300.)?
        .draw_histogram_vertical(
            data.iter().enumerate().map(|(i, v)| (i as f64, *v as f64)),
            show_image::RED.mix(1.),
            "histogram",
        )?;

    Ok(())
}
